import java.awt.event.KeyEvent;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaLog.java
 *
 * Created on 29/04/2012, 09:14:26
 */

/**
 *
 * @author Wagner
 */
public class JanelaLog extends javax.swing.JFrame {

	private static final long serialVersionUID = -849339034915359391L;
	private static JanelaLog instance;
	private static ClienteSocketSample conexao;

	/** Creates new form JanelaLog */
	public JanelaLog(ClienteSocketSample conexao) {
		initComponents();
		JanelaLog.conexao = conexao;
	}

	public static JanelaLog getInstance(ClienteSocketSample conexao) {
		if(instance==null){
			JanelaLog.instance = new JanelaLog(conexao);
		}
		return instance;
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		log_in = new javax.swing.JTextArea();
		enviar_btn = new javax.swing.JButton();
		msg_in = new javax.swing.JTextField();
		msg_label = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Cliente"); // NOI18N
		setName("Form"); // NOI18N

		jScrollPane1.setName("jScrollPane1"); // NOI18N

		log_in.setColumns(20);
		log_in.setEditable(false);
		log_in.setRows(5);
		log_in.setName("log_in"); // NOI18N
		jScrollPane1.setViewportView(log_in);

		enviar_btn.setText("Enviar"); // NOI18N
		enviar_btn.setName("enviar_btn"); // NOI18N
		enviar_btn.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				enviar_btnActionPerformed(evt);
			}
		});

		msg_in.setText(""); // NOI18N
		msg_in.setName("msg_in"); // NOI18N

		msg_label.setText("Mensagem:"); // NOI18N
		msg_label.setName("msg_label"); // NOI18N
		msg_in.addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyPressed(java.awt.event.KeyEvent evt) {
				msg_inKeyPressed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
								.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 715, Short.MAX_VALUE)
								.addGroup(layout.createSequentialGroup()
										.addComponent(msg_label)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(msg_in, javax.swing.GroupLayout.DEFAULT_SIZE, 587, Short.MAX_VALUE)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(enviar_btn)))
										.addContainerGap())
				);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(msg_in, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(enviar_btn)
								.addComponent(msg_label))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 432, Short.MAX_VALUE)
								.addContainerGap())
				);

		pack();
	}// </editor-fold>

	private void enviar_btnActionPerformed(java.awt.event.ActionEvent evt) {
		enviar();
	}

	private void enviar() {
		conexao.sendMessage(msg_in.getText());
	}

	private void msg_inKeyPressed(java.awt.event.KeyEvent evt) {

		int key = evt.getKeyCode();
		if (key == KeyEvent.VK_ENTER) {
			enviar();
		}
	}

	public void log(String msg) {
		log_in.setText(log_in.getText()+"\n"+ msg);
		msg_in.setText("");
	}

	// Variables declaration - do not modify
	private javax.swing.JButton enviar_btn;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTextArea log_in;
	private javax.swing.JTextField msg_in;
	private javax.swing.JLabel msg_label;
	// End of variables declaration
}
